using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;

public partial class User_MyArticles : System.Web.UI.Page
{
    private int userId;
    private Database db;

    protected void Page_Load(object sender, EventArgs e)
    {
        SessionHelper<int>.setVar("UserId", 1);
        dgMyArticles.ItemDataBound += new DataGridItemEventHandler(dgMyArticles_ItemDataBound);
        if (SessionHelper<int>.existsVar("UserId"))
        {
            userId = SessionHelper<int>.getVar("UserId");
            db = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["ConnectionStringName"]);
            showMyArticles(userId);
        }
        else
        {
            Response.Redirect("http://www.google.com");
        }
    }

    void dgMyArticles_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        string statusid = "";
        if((DataRowView)e.Item.DataItem != null)
             statusid = ((DataRowView)e.Item.DataItem).Row["articlestatusid"].ToString();

         Image img = (Image)e.Item.FindControl("imgStatus");
         HyperLink hl = (HyperLink)e.Item.FindControl("hlEdit");
         if (img != null)
         {
             switch (statusid)
             {
                 case "1":
                     {
                         img.ImageUrl = WebTool.getWebRoot() + "/Images/ArticleDraft.gif";
                     }
                     break;
                 case "2":
                     {
                         img.ImageUrl = WebTool.getWebRoot() + "/Images/ArticleWaiting.gif";
                     }
                     break;
                 case "3":
                     {
                         img.ImageUrl = WebTool.getWebRoot() + "/Images/ArticlePubliched.gif";
                     }
                     break;
                 case "4":
                     {
                         img.ImageUrl = WebTool.getWebRoot() + "/Images/ArticleDeclined.gif";
                     }
                     break;
                 case "5":
                     {
                         img.ImageUrl = WebTool.getWebRoot() + "/Images/ArticleNotReady.gif";
                     }
                     break;
                 default:
                     {
                         img.ImageUrl = WebTool.getWebRoot() + "/Images/ArticleDraft.gif";
                     }
                     break;
             }
         }
         if (hl != null)
         {
             hl.NavigateUrl = WebTool.getWebRoot() + "/blabla/?articleid=" + ((DataGrid)sender).DataKeyField[e.Item.ItemIndex];
         }
    }

    

    private void showMyArticles(int userId)
    {           
            DbCommand cmd = db.GetStoredProcCommand("ArticlesByUser");
            cmd.Parameters.Add(new SqlParameter("UserId",userId));
            DataSet ds = db.ExecuteDataSet(cmd);
            dgMyArticles.DataSource = ds;
            dgMyArticles.DataBind();        
    }
}
